﻿using Dapper;
using System;
using BCEntity.HelpDocument.HelpDocumentCategory;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.HelpDocument.HelpDocumentCategory
{
    /// <summary>
    /// 帮助文档类型
    /// </summary>
    public class HelpDocumentCategoryData : IHelpDocumentCategoryData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM helpdocumentcategory";
        private const string QUERY = @"SELECT HelpDocumentCategoryId,DocumentCategoryName,CreateTime,CreateAdminId,CreateAdminName,UpdateTime,EditAdminId,EditAdminName FROM helpdocumentcategory";
        public HelpDocumentCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查帮助文档类型是否存在
        public async Task<bool> Exists(long helpDocumentCategoryId)
        {
            string sql = string.Format(@"{0} WHERE HelpDocumentCategoryId=?HelpDocumentCategoryId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?HelpDocumentCategoryId", helpDocumentCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加帮助文档类型
        public async Task<HelpDocumentCategoryEntity> Add(HelpDocumentCategoryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("helpdocumentcategory", new string[] { "DocumentCategoryName", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE HelpDocumentCategoryId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?DocumentCategoryName", entity.DocumentCategoryName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<HelpDocumentCategoryEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改帮助文档类型
        public async Task<HelpDocumentCategoryEntity> Update(HelpDocumentCategoryEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("helpdocumentcategory", new string[] { "DocumentCategoryName", "UpdateTime", "EditAdminId", "EditAdminName" }, new string[] { "HelpDocumentCategoryId" });
            sql = string.Format("{0} {1} WHERE HelpDocumentCategoryId=?HelpDocumentCategoryId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?HelpDocumentCategoryId", entity.HelpDocumentCategoryId, DbType.Int64, size: 20);
            parameters.Add("?DocumentCategoryName", entity.DocumentCategoryName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?UpdateTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<HelpDocumentCategoryEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除帮助文档类型
        public async Task<bool> Delete(long helpDocumentCategoryId)
        {
            string sql = @"DELETE FROM helpdocumentcategory WHERE HelpDocumentCategoryId=?HelpDocumentCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?HelpDocumentCategoryId", helpDocumentCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取帮助文档类型
        public async Task<HelpDocumentCategoryEntity> GetEntity(long helpDocumentCategoryId)
        {
            string sql = string.Format(@"{0} WHERE HelpDocumentCategoryId=?HelpDocumentCategoryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?HelpDocumentCategoryId", helpDocumentCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<HelpDocumentCategoryEntity>(sql, parameters);
        }
        #endregion

        #region 获取帮助文档类型列表
        public async Task<IEnumerable<HelpDocumentCategoryEntity>> GetList()
        {
            return await this.databaseContext.Slave.QueryAsync<HelpDocumentCategoryEntity>(QUERY);
        }
        #endregion

        #region 获取帮助文档类型分页
        public async Task<(IEnumerable<HelpDocumentCategoryEntity>, int)> Query(string documentCategoryName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (!string.IsNullOrWhiteSpace(documentCategoryName))
            {
                wheres.Add("DocumentCategoryName LIKE CONCAT('%',CONCAT(?DocumentCategoryName,'%'))");
                parameters.Add("?DocumentCategoryName", documentCategoryName?.Trim(), DbType.AnsiString);
            }

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM helpdocumentcategory {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<HelpDocumentCategoryEntity, int>(sql, parameters);
        }
        #endregion
    }
}



